package com.jiinfo.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.jiinfo.config.ConstConfig;
import com.jiinfo.model.FileInfo;

public class BaseController extends Controller{

	public HttpSession getHttpSession(){
		return super.getSession();
	}
	
	public FileInfo addFile(String fileParam,String userId){
		String path=PropKit.get("file.storePath");
		UploadFile uploadFile=getFile(fileParam, path);
		if(uploadFile==null){
			return null;
		}
		FileInfo fileInfo=FileInfo.dao.add(uploadFile,userId);
		return fileInfo;
	}
	
	public UploadFile uploadFile(String fileParam){
		String path=PropKit.get("file.storePath");
		UploadFile uploadFile=getFile(fileParam, path);
		if(uploadFile==null){
			return null;
		}
		return uploadFile;
	}
	
	public Session getCurrSession(){
		Subject subject=SecurityUtils.getSubject();
		return subject.getSession();
	}
	
	public Record getCurrUser(){
		Session session=getCurrSession();
		return (Record)session.getAttribute(ConstConfig.SESSION_USER);
	}
	
	public List<Record> getListForTree(String chilaName,Record record,List<Record> list){
		list.add(record);
		@SuppressWarnings("unchecked")
		List<Record> childs=(List<Record>)record.get(chilaName);
		if(childs.size()>0){
			for (Record record2 : childs) {
				getListForTree(chilaName,record2,list);
			}
		}
		return list;
	}
	
	
	public Map<String,Object> getPageTitle(String title,String smallTitle,String[] breadcrumbNames,String[] breadcrumbs){
		Map<String,Object> map=new HashMap<>();
		map.put("title", title);
		map.put("smallTitle", smallTitle);
		
		List<Map<String,String>> list=new ArrayList<>();
		for (int i=0;i<breadcrumbNames.length;i++) {
			Map<String,String> map2=new HashMap<>();
			map2.put("name", breadcrumbNames[i]);
			map2.put("url", breadcrumbs[i]);
			list.add(map2);
		}
		map.put("breadcrumbs", list);
		
		return map;
	}
}
